# -*- coding: utf-8 -*-    
from django.shortcuts import render_to_response, get_object_or_404
import models

def project_list(request):
    return render_to_response('project_list.html', {
        'projects': models.Project.objects.all(), 
    })        

def user_list(request):
    return render_to_response('user_list.html', {
        'users': models.User.objects.all(), 
    })        

#def project_details(request, project_id):
#    project = get_object_or_404(models.Project, id=project_id)
#    return render_to_response('project_details.html', {
#        'project': project,
#    })


def free_tasks(tasks):
    return tasks.filter(
        executor__isnull=True,
        close_datetime__isnull=True,
    ))

def inprocess_tasks(tasks):
    return tasks.filter(
        executor__isnull=False,
        close_datetime__isnull=True,
    ))

def done_tasks(tasks):
    return tasks.filter(
        close_datetime__isnull=False,
    ))


def task_list(request, project_id=None, user_id=None): 
    context = {}
    tasks = models.Task.objects.all()

    if project_id:
        project = get_object_or_404(models.Project, id=project_id)
        tasks = tasks.filter(project=project)
        context['project'] = project

    columns = []
    if user_id:
        user = get_object_or_404(models.User, id=user_id)
        context['user'] = user
        #columns.append((u'Автор задач', tasks.filter(author=user)))
        columns.append((u'В работе', inprocess_tasks(tasks).filter(executor=user)))
        columns.append((u'Готово', done_tasks(tasks).filter(executor=user)))
    else:
        columns.append((u'Ничьё', free_tasks(tasks)))
        columns.append((u'В работе', inprocess_tasks(tasks)))
        columns.append((u'Готово', done_tasks(tasks)))
    context['columns'] = columns
    context['column_size'] = int(100 / len(columns))    
    return render_to_response('task_list.html', context)

def task_details(request, task_id):
    return render_to_response('task_details.html', {
        'task': get_object_or_404(models.Task, id=task_id),
    })
        